package com.example.java_gobang.config;

import com.example.java_gobang.api.GameAPI;
import com.example.java_gobang.api.MatchAPI;
import com.example.java_gobang.api.TestAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 * websocket配置类：
 * 注册websocket的处理类，设置路径及配置登录拦截器
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    // 注入测试的websocket处理器
    @Autowired
    private TestAPI testAPI;

    // 注入匹配功能的websocket处理器
    @Autowired
    private MatchAPI matchAPI;

    // 注入游戏功能的websocket处理器
    @Autowired
    private GameAPI gameAPI;

    /**
     * 注册websocket处理器
     * @param webSocketHandlerRegistry websocket处理器注册对象
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        // 注册测试功能websocket
        webSocketHandlerRegistry.addHandler(testAPI, "/test");
        // 注册匹配功能websocket及登录拦截器
        webSocketHandlerRegistry.addHandler(matchAPI, "/findMatch")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
        // 注册游戏功能websocket及登录拦截器
        webSocketHandlerRegistry.addHandler(gameAPI, "/game")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
